<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
use DB;
class AddressController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $id = Auth::user()->id;
        $address = DB::table('address')->where('uid',$id)->get();
        return view('home.address')->with('id',$id)->with('addr',$address);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $res = DB::table('address')->insertGetId([
            'name'=>$request->name,
            'uid'=>$request->uid,
            'phone'=>$request->phone,
            'address'=>$request->address
        ]);
        return $res;
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.地址删除
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        DB::table('address')->where('id',$id)->delete();
        return redirect('/address');   
    }

    //地址状态修改
    public function status(Request $request)
    {
        // echo $request->id;
        $uid = Auth::user()->id;
        DB::table('address')->where('uid',$uid)->update(['status'=>0]);
       return $res = DB::table('address')->where('id',$request->id)->update(['status'=>1]);
    }
}
